package com.example.dao;

import java.util.List;
import java.util.Map;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.example.domain.User;

/**
 * @author 袁小弘
 * @describe 用户DAO层
 * @date 2019-12-31
 */
@Repository
public interface UserDao extends JpaRepository<User, Integer> {

    User findByUserName(String userName);

    User findByUserId(Integer user_Id);//根据用户编号查询用户信息
    //User getByUserNameAndPassword(String userName,String password);

    @Transactional
    @Modifying
    int deleteByUserId(Integer user_Id);//根据用户编号删除用户信息

    @Query(nativeQuery = true, value = "SELECT u.* from users u left JOIN passwd p on u.user_Id=p.user_Id WHERE u.account=?1 and p.password=?2")
    public User getByAccountAndPassword(String account, String password);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as id,u.account as account,p.password as pwd from users u left JOIN passwd p on u.user_Id=p.user_Id WHERE u.account=?1 and p.password=?2")
    public Map<String, Object> getTwoByAccountAndPassword(String account, String password);

    @Query(nativeQuery = true, value = "SELECT u.user_Id as userId,u.account as userName,u.nickName FROM users u")
    List<Map<String, Object>> findByUserNameAndUserId();//查询所有用户账号和ID==>用于密码管理

    @Transactional
    @Modifying
    int deleteByUserIdIn(List<Integer> listId);//根据用户编号批量删除用户

    List<User> findByUserNameLike(String string);//根据用户账号模糊查询

    List<User> findByNickNameLike(String string);//根据用户昵称模糊查询

    List<User> findByTelephoneLike(String string);//根据用户手机号码模糊查询

    List<User> findByEmailLike(String string);//根据用户邮箱模糊查询


}
